﻿using System;
using System.Collections.Generic;
using ZX.BLL;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.CRM.Library;
using ZX.Web.UI.Controls.ControlBind;
using System.Data;

namespace ZX.WebApp.Crm.Sale
{
    public partial class AddOrderField : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        BTCrmCrustomerOrder bllOrder = new BTCrmCrustomerOrder();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BTCrmTeachHodometer bllTeacherHodometer = new BTCrmTeachHodometer();
        string strFlag = "", strBtnName = "M";
        protected void Page_Load(object sender, EventArgs e)
        {
            strFlag = this.GetStringFromQueryString("flag", "");
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string id = this.GetStringFromQueryString("id", "");
            string cid = this.GetStringFromQueryString("cid", "");//转约场订单
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(id) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);

            #region----------
            //付款方式
            IList<DatPhraseDetail> ListPaymentId = bllDetail.GetDatPhraseDetailByClassValue("PaymentID");
            DropDownListControl.DropDataSourceBind(this.PaymentId, ListPaymentId, "PhraseText", "PhraseID");
            //订单状态
            IList<DatPhraseDetail> ListStateId = bllDetail.GetDatPhraseDetailByClassValue("OrderStateID");
            DropDownListControl.DropDataSourceBind(this.StateID, ListStateId, "PhraseText", "PhraseID");
            //订单类型
            IList<DatPhraseDetail> ListTypeId = bllDetail.GetDatPhraseDetailByClassValue("OrderType");
            DropDownListControl.DropDataSourceBind(this.TypeID, ListTypeId, "PhraseText", "PhraseID");
            //币别
            IList<DatPhraseDetail> ListValuteId = bllDetail.GetDatPhraseDetailByClassValue("ValuteID");
            DropDownListControl.DropDataSourceBind(this.ValutaID, ListValuteId, "PhraseText", "PhraseID");
            #endregion

            DataTable dts = bllOrder.GetDeptByUserId(new Guid(this.UserId.ToString()));
            if (dts != null && dts.Rows.Count > 0)
            {
                string tempDeptId = dts.Rows[0][2].ToString(); //部门ID
                if (tempDeptId.ToUpper() == "30E5C699-40BC-4721-A0AC-FEF31C9E8F4C") //渠道部
                {
                    strBtnName = "QD";
                }
            }


            if (DataAction == "insert" || strFlag == "0")
            {
                this.Code.Value = IDMaker.GetSequenceStr("BILL005");
            }
            if (strFlag == "0") //新增
            {
                this.ID.Value = Guid.NewGuid().ToString();

                DataTable dt = bllOrder.GetDeptByUserId(new Guid(this.UserId.ToString()));
                if (dt != null && dt.Rows.Count > 0)
                {
                    this.DeptId.Value = dt.Rows[0][0].ToString();
                    this.txtDeptId.Value = dt.Rows[0][1].ToString();
                }
                
                if (cid != "")
                {
                    V_CrmCustomer customer = bll.GetCustomerInfo(new Guid(cid));
                    if (customer != null)
                    {
                        this.CustomerCode.Value = customer.ID.ToString();
                        this.txtCustomerCodes.Value = customer.Code;
                        this.CustomerName.Value = customer.CustomerName;
                        this.CustomerType.Value = customer.CustomerType;
                        this.PersonId.Value = customer.PersonCount;
                        this.TradeId.Value = customer.CustomerTrade;
                        this.CreateDate.Value = customer.CompanyCreateDate.ToString("yyyy-MM-dd");
                        this.CompanyProduct.Value = customer.CompanyProduct;
                        this.SaleadName.Value = customer.SaleadName;
                        this.AddUserName.Value = customer.UName;
                    }
                }
            }
            else  //变更
            {
                dataBinds(id);
            }
        }
        private void dataBinds(string id) 
        {
            this.ID.Value = id;
            V_CrmCustomerOrder Orders = bllOrder.GetAllCrmCustomerOrder(new Guid(id));
            #region--------------
            if (Orders != null)
            {
                this.Code.Value = Orders.Code;
                this.CustomerCode.Value = Orders.CustomerId.ToString();
                this.Date.Value = Orders.Date.ToString("yyyy-MM-dd");
                this.txtCustomerCodes.Value = Orders.CustomerCode;
                this.CustomerName.Value = Orders.CustomerName;
                this.CustomerType.Value = Orders.CustomerType;
                this.PersonId.Value = Orders.CustomerPerson;
                this.TradeId.Value = Orders.CustomerTrade;
                this.CreateDate.Value = Orders.CompanyCreateDate.ToString("yyyy-MM-dd");
                this.CompanyProduct.Value = Orders.CompanyProduct;
                this.DeptId.Value = Orders.DeptId.ToString();
                this.txtDeptId.Value = Orders.DeptName;
                this.AddUserName.Value = Orders.MessageName;
                this.SaleadName.Value = Orders.SaleadName;
                this.TotalMoney.Value = Orders.TotalMoney.ToString();
                this.Explain.Value = Orders.Explain;

                V_CrmTeachHodometer teacherHodometer = bllTeacherHodometer.GetTeacherHodometerByCustomerId(Orders.CustomerId);
                if (teacherHodometer != null)
                {
                    this.txtTeacherAddress.Value = teacherHodometer.EXP1;
                    this.txtTeacherTime.Value = teacherHodometer.HmDate.ToString("yyyy-MM-dd");
                    DropDownListControl.DropDownListSelect(this.drpTeacherType, teacherHodometer.Expr1);
                    this.txtTeacherId.Value = teacherHodometer.teachname;
                    this.TeacherId.Value = teacherHodometer.TeachID.ToString();
                }

                IList<V_CrmLinkMan> listLinkMan = bll.GetJieQiaRenInfoByCustomerId(Orders.CustomerId);
                if (listLinkMan != null && listLinkMan.Count > 0)
                {
                    this.LinkManId.Value = listLinkMan[0].ID.ToString();
                    this.LinkManName.Value = listLinkMan[0].Caption;
                    DropDownListControl.DropDownListSelect(this.LinkManSex, listLinkMan[0].Sex);
                    this.LinkManPosition.Value = listLinkMan[0].Position;
                    this.LinkManTel.Value = listLinkMan[0].MobileTel;
                }

                IList<V_CrmOrderPerson> orderPerson = bllOrder.GetOrderPerson(new Guid(id), "ZhuYueRenID");
                if (orderPerson != null && orderPerson.Count > 0)
                {
                    this.ZhuYueRenID.Value = orderPerson[0].UserId.ToString();
                    this.txtZhuYueRenID.Value = orderPerson[0].UserName + "(" + orderPerson[0].EmployeeCode + ")";
                }

            }
            DropDownListControl.DropDownListSelect(this.PaymentId, Orders.PaymentId.ToString());
            DropDownListControl.DropDownListSelect(this.TypeID, Orders.TypeID.ToString());
            DropDownListControl.DropDownListSelect(this.ValutaID, Orders.ValutaID.ToString());
            DropDownListControl.DropDownListSelect(this.StateID, Orders.StateID.ToString());
            #endregion
        }

        #region-----------
        /// <summary>
        /// 标题
        /// </summary>
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", ""); } }
        /// <summary>
        /// 状态
        /// </summary>
        public string Flag
        { get { return strFlag; } }
        /// <summary>
        /// 获取当前用户名
        /// </summary>
        public string UName
        { get { return this.UserName; } }
        /// <summary>
        /// 当前用户ID
        /// </summary>
        public string UserID
        { get { return this.UserId.ToString(); } }
        /// <summary>
        /// 按钮
        /// </summary>
        public string BtnName
        { get { return strBtnName; } }
        #endregion
    }
}